package com.tansun.easycare.rule.rulemange.entity;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;

import org.hibernate.validator.constraints.Length;

import com.tansun.easycare.rule.common.entity.RuleBaseEntity;
import com.tansun.easycare.rule.exception.BizException;

/**
 * 规则管理Entity
 *
 * @author lxs
 * @version 2017-02-23
 */
public class RuleManage extends RuleBaseEntity<RuleManage> implements Serializable {

    private static final long serialVersionUID = 1L;
    private String code;        // 规则模型编号
    private String name;        // name
    private String modelType;        // 规则模型类型
    private String projectCode;        // 规则库编码
    private String status;        // 规则模型状态
    private String version;        // 规则模型版本号
    private String isSingle;        // 是否单一输出
    private String activeVersion;  //当前有效版本
    private String activeFlag;    //是否有效标识符 :传值0查询更新列表的List，传值1查询最新且有效的List
    private String users;   //修改者
	private Integer countSalience;

    private String isTemp;

    private String oldVersion;   //老版本

    private String oldId;//老ID
 

    public RuleManage() {
        super();
    }

    public RuleManage(RuleManage rule) {
        super();
        this.setName(rule.getName() + "_copy");
//		this.setVersion(rule.getVersion());
        this.setModelType(rule.getModelType());
        this.setProjectCode(rule.getProjectCode());
    }

    public RuleManage(String id) {
        super(id);
    }

    @Length(min = 0, max = 50, message = "规则模型编号长度必须介于 0 和 50 之间")
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Length(min = 0, max = 50, message = "name长度必须介于 0 和 50 之间")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Length(min = 0, max = 50, message = "规则模型类型长度必须介于 0 和 50 之间")
    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    @Length(min = 0, max = 50, message = "规则库编码长度必须介于 0 和 50 之间")
    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    @Length(min = 0, max = 10, message = "规则模型状态长度必须介于 0 和 10 之间")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Length(min = 0, max = 10, message = "规则模型版本号长度必须介于 0 和 10 之间")
    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Length(min = 0, max = 10, message = "是否单一输出长度必须介于 0 和 10 之间")
    public String getIsSingle() {
        return isSingle;
    }

    public void setIsSingle(String isSingle) {
        this.isSingle = isSingle;
    }

    public String getIsTemp() {
        return isTemp;
    }

    public void setIsTemp(String isTemp) {
        this.isTemp = isTemp;
    }
    public void initVersion() throws BizException{
    	initVersion(null);
    }
    public void initVersion(String isTmp) throws BizException {
//    	if(RuleModelTypeEnum.DataMining.getCode().equals(this.getModelType())) {
//    		return;
//    	}
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if ("2".equals(this.getStatus())) //发布
        {
            String verStr = this.getVersion().substring(1, this.getVersion().length() - 3);
            float version = Float.parseFloat(verStr);
            if (this.getVersion().matches("v\\d+\\.00")) {
                throw new BizException("E001","已经是发布版本，不能重复发布");
            }
            version = version + 1;
            this.setVersion("v" + decimalFormat.format(version));
        } else if ("1".equals(this.getStatus())) //修改
        {
            String verStr = this.getVersion().substring(1, this.getVersion().length());
            if (verStr != null) {
                int idx = verStr.indexOf("-tmp");
                if (idx > 0)
                    verStr = verStr.substring(0, idx);
            }
            float version = Float.parseFloat(verStr);
            version = (float) (version + 1);
            this.setVersion("v" + decimalFormat.format(version));
            this.setStatus("1");
        }
        if (isTmp != null && "true".equals(isTmp)) {
        	if(this.getVersion().indexOf("-tmp")>-1){
        		String verStr = this.getVersion().substring(0, this.getVersion().indexOf("-tmp"));
                float version = Float.parseFloat(verStr);
                version = (float) (version + 0.01);
                this.setVersion("v" + decimalFormat.format(version));
        	}
        	this.setVersion(this.getVersion()+"-tmp");
        }
    }
    
    

	public String getOldVersion() {
        return oldVersion;
    }

    public void setOldVersion(String oldVersion) {
        this.oldVersion = oldVersion;
    }

    public Integer getCountSalience() {
        return countSalience;
    }

    public void setCountSalience(Integer countSalience) {
        this.countSalience = countSalience;
    }

    public String getOldId() {
        return oldId;
    }

    public void setOldId(String oldId) {
        this.oldId = oldId;
    }

	public String getActiveVersion() {
		return activeVersion;
	}

	public void setActiveVersion(String activeVersion) {
		this.activeVersion = activeVersion;
	}

	public String getActiveFlag() {
		return activeFlag;
	}

	public void setActiveFlag(String activeFlag) {
		this.activeFlag = activeFlag;
	}



    public String getUsers() {
        return users;
    }

    public void setUsers(String users) {
        this.users = users;
    }
}